<?php
namespace app\wechat\controller;
use think\Controller;
use think\Db;
use think\Request;
class Gift extends Controller
{
    protected $table_user_sms_info= 'user_sms_info';         /* 验证码信息表 */
    protected $table_user_bind_info= 'user_bind_info';       /* 绑定手机号 */
    protected $table_accounts_info= 'accounts_info';         /* 账户表 */
    protected $table_user_gift_record= 'user_gift_record';   /* 用户兑换表 */
    protected $table_user_score_log= 'user_score_log';       /*用户金币记录表*/
    protected $table_user_prop_log= 'user_prop_log';         /*用户道具记录表*/
    protected function Db_connect_user_sms_info(){
        $user_sms_info= Db::connect('db_qpplatformdb')->table($this->table_user_sms_info);
        return $user_sms_info;
    }
    protected function Db_connect_user_gift_record(){
        $user_gift_record= Db::connect('db_qpplatformdb')->table($this->table_user_gift_record);
        return $user_gift_record;
    }
    protected function Db_connect_accounts_info(){
        $accounts_info= Db::connect('db_config2')->table($this->table_accounts_info);
        return $accounts_info;
    }
    protected function Db_connect_user_bind_info(){
        $user_bind_info= Db::connect('db_config2')->table($this->table_user_bind_info);
        return $user_bind_info;
    }
    protected function Db_connect_table_accounts_info(){
        $accounts_info= Db::connect('db_config2')->table($this->table_accounts_info);
        return $accounts_info;
    }
    protected function Db_connect_user_score_log(){
        $user_score_log= Db::connect('db_config3')->table($this->table_user_score_log);
        return $user_score_log;
    }
    protected function Db_connect_user_prop_log(){
        $user_prop_log= Db::connect('db_config3')->table($this->table_user_prop_log);
        return $user_prop_log;
    }
    public function index()
    {
        $user=$this->checkLogin();
        $this->assign('user',$user);
        return  $this->fetch();
    }
    function exchange(){
         if($this->request->isPost()){
              $data = $this->request->post();
              if($data['phone']==''){
                  return $this->error('兑换手机号不能为空');
              }
              $data_arr['UserID']=session('UserID');
              $user_arr =$this->Db_connect_accounts_info()->field("VipLevel,Lottery")->where("UserID",session('UserID'))->find();
              if($user_arr['VipLevel']>=2){
                  if($user_arr['Lottery']>=$data['lottery']){
                      $res =$this->Db_connect_accounts_info()->where("UserID",session('UserID'))->setDec('Lottery',$data['lottery']);
                      if($res){
                          $data_arr['Icon']=$this->exchange_type($data['type'])['Icon'];
                          $data_arr['Phone']=$data['phone'];
                          $data_arr['Address']=$data['phone'];
                          $data_arr['State']=0;
                          $data_arr['InsertTime']=date('Y-m-d H:i:s',time());
                          $data_arr['Describle']=$this->exchange_type($data['type'])['Describle'];
                          $re =$this->Db_connect_user_gift_record()->insert($data_arr);
                          if($re){
                              $res =$this->user_score_log(session('UserID'),$data['lottery']);
                              if($res){
                                  $this->user_prop_log(session('UserID'));
                                  return $this->success('兑换成功,添加微信hemabuyu888可以查看兑换进度哦');
                              }
                          }
                      }
                  }else{
                      return $this->error('兑换失败,鱼劵不足');
                  }
              }else{
                  return $this->error('VIP等级达到2级才能兑换');
              }
        }
    }
    function exchange_type($type){
            switch ($type) {
                case 1:
                    $arr =[
                        'Describle'=>'30元话费',
                        'Icon'=>'changeIcon_5.png'
                    ];
                    return $arr;
                    break;
                case 2:
                    $arr =[
                        'Describle'=>'100元京东卡',
                        'Icon'=>'changeIcon_11.png'
                    ];
                    return $arr;
                    break;
            }
    }
    /**
     * 充值道具日志记录
     * @author zhangzhiyuan
     * @version 2019-03-18
     * @param string
     * @return string json
     */
    function  user_prop_log($UserID){
            $data['UserID']=$UserID;
            $data['ServerID']=0;
            $data['GameName']='大厅';
            $data['GameLevel']=0;
            $data['PropID']=100;
            $data['Counts']=-1;
            $data['VarCounts']=1;
            $data['Type']='礼品兑换';
            $data['RecordTime']=date('Y-m-d H:i:s',time());
            $re =$this->Db_connect_user_prop_log()->insert($data);
            if($re)return true;
    }
    /**
     * 金币鱼卷记录
     * @author zhangzhiyuan
     * @version 2019-03-18
     * @param string
     * @return string json
     */
    function  user_score_log($UserID,$VarLottery=0){
            $arr =$this->Db_connect_accounts_info()->field('Score,Lottery,LotteryStock,TotalRecharge,Diamond')->where('UserID',$UserID)->find();
            $data['UserID']=$UserID;
            $data['SpreaderID']=0;
            $data['ServerID']=0;
            $data['GameName']='大厅';
            $data['GameLevel']=0;
            $data['VarScore']=0;
            $data['Score']=$arr['Score'];
            $data['VarLottery']=$VarLottery;
            $data['Lottery']=$arr['Lottery'];
            $data['Stock']=$arr['LotteryStock'];
            $data['VarStock']=0;
            $data['Type']='礼品兑换';
            $data['RecordTime']=date('Y-m-d H:i:s',time());
            $data['VarDiamond']=0;
            $data['Diamond']=$arr['Diamond'];
            $re = $this->Db_connect_user_score_log()->insert($data);
            if($re) return true;
    }
    function checkLogin()
    {
        if(!session('UserID'))
        {
            $this->redirect(url('user/index'));
        }else{
            $data = $this->Db_connect_table_accounts_info()->where('UserID',session('UserID'))->find();
            return $data;
        }
    }
    function success_re(){
        $arr=$this->Db_connect_table_accounts_info()->where('UserID',session('UserID'))->find();
        $this->assign('arr',$arr);
        return  $this->fetch();
    }

}
